package com.lgx.mall.business.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.mall.business.dao.IOrderDao;
import com.lgx.mall.business.dao.IOrderDetailDao;
import com.lgx.mall.business.entity.Order;
import com.lgx.mall.business.entity.OrderDetail;
import com.lgx.mall.business.service.IOrderService;
import com.lgx.mall.core.dao.IProductDao;
import com.lgx.mall.core.entity.Product;
import com.lgx.right.dao.IUserDao;
import com.lgx.right.entity.User;

@Service
public class OrderService implements IOrderService{
	
	@Autowired
	private IOrderDao orderDao;
	
	@Autowired
	private IOrderDetailDao orderDetailDao;
	
	@Autowired
	private IUserDao userDao;
	
	@Autowired
	private IProductDao productDao;

	public IProductDao getProductDao() {
		return productDao;
	}

	public void setProductDao(IProductDao productDao) {
		this.productDao = productDao;
	}

	public IOrderDetailDao getOrderDetailDao() {
		return orderDetailDao;
	}

	public void setOrderDetailDao(IOrderDetailDao orderDetailDao) {
		this.orderDetailDao = orderDetailDao;
	}

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public IOrderDao getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(IOrderDao orderDao) {
		this.orderDao = orderDao;
	}

	/**
	 * 查询
	 */
	@Override
	public Pagination findOrderPagination(String username, String usercard,
			String orderNumber, String starTime, String endTime,String status, int pageSize,
			int pageNum) {
		return orderDao.findOrderPagination(username, usercard, orderNumber, starTime, endTime,status, pageSize, pageNum);
	}

	@Override
	public Order findOrderById(Long orderId) {
		return this.orderDao.findOrderById(orderId);
	}

	@Override
	public void deleteOrder(Order order) {
		this.orderDao.deleteOrder(order);
	}

	@Override
	public void updateOrder(Order order) {
		this.orderDao.updateOrder(order);
	}

	@Override
	public Order createOrder(Order order) {
		return this.orderDao.createOrder(order);
	}

	@Override
	public Order findOrderByUserId(long userId) {
		
		Order order = this.orderDao.findOrderByUserId(userId);
		
		if(order == null){   //如果没有找到这个订单，表示还未被创建，便创建一个新订单
			order = new Order();
			User user = this.userDao.findUserById(userId);
			order.setUser(user);
			order.setStatus(0);
			order.setAmount(0);   //这个订单中的商品数量
			order.setScore(0.0);  //需要花费的总积分
			this.orderDao.createOrder(order);
		}
		
		return order;
	}

	@Override
	public Order addOrderDetail(long userId, String entityId, String entityType,String description) {
		Order order = this.findOrderByUserId(userId);
		
		double price = 0.0;
		
		//商品
		if("com.lgx.mall.core.entity.Product".equals(entityType)){
			//获取商品
			Product product = this.productDao.findProductById(Long.parseLong(entityId));
			price = product.getPrice();
			//查询订单中是否已有该商品
			OrderDetail detail = this.orderDetailDao.findOrderDetailById(order.getId(), entityId, entityType);
			if(detail == null){
				//创建
				detail = new OrderDetail();
				detail.setType("1");
				detail.setEntityType(entityType);
				detail.setEntityId(Long.parseLong(entityId));
				detail.setEntityName(product.getName());
				detail.setEntityUrl("product/"+product.getId()+".htm");
				detail.setEntityImg(product.getImageUrl1());
				detail.setDescription(description);
				detail.setAmount(1);
				detail.setPrice(price);
				detail.setOrder(order);
				
				try {
					this.orderDetailDao.createOrderDetail(detail);
				} catch (DataException e) {
					e.printStackTrace();
				}
				
			}else{
				//更新
				int amount = detail.getAmount();
				amount++;
				detail.setAmount(amount);
				
				try {
					this.orderDetailDao.updateOrderDetail(detail);
				} catch (DataException e) {
					e.printStackTrace();
				}
			}
		}
		
		//更新订单
		order.setScore(order.getScore() + price);
		order.setAmount(order.getAmount() + 1);
		order.setShoppingTime(new Date());
		
		this.orderDao.updateOrder(order);
		
		return order;
	}

	@Override
	public Order findOrderByOrderNo(String orderNo) {
		return this.orderDao.findOrderByOrderNo(orderNo);
	}

	@Override
	public Pagination findOrderPagination(Long userId, int pageSize, int pageNum) {
		return this.orderDao.findOrderPagination(userId, pageSize, pageNum);
	}
}
